<?php

namespace App\Http\Resources\goods;

use App\Models\goods\Goods;
use App\Models\goods\OrderGroup;
use Illuminate\Http\Resources\Json\Resource;

class GoodsDetailResource extends Resource
{
    protected $is_collection;

    public function __construct(Goods $resource,$is_collection = 0)
    {
        $this->resource = $resource;
        $this->is_collection = $is_collection;
    }

    /**
     * Transform the resource into an array.
     *
     * @param  \Illuminate\Http\Request $request
     * @return array
     */
    public function toArray($request)
    {
        $specs = [];
        foreach ($this->resource->specs as $spec) {

            $specs[] = [
                'goods_id' => $spec['goods_id'],
                'id' => $spec['id'],
                'name' => $spec['name'],
                'price' => $spec['price'],
                'shop_price' => $spec['shop_price'],
                'business_price' => $spec['business_price'],
                'pt_price' => $spec['pt_price'],
                'xql_price' => $spec['xql_price'],
                'sr_price' => $spec['sr_price'],
            ];
        }
        $groups = [];
        foreach ($this->resource->order_groups as $group) {
            if ($group instanceof OrderGroup);

            $groups[] = [
                'id' => $group->id,
                'total' => $group->total,
                'num' => $group->num,
                'sub' => $group->total - $group->num,
                'user' => [
                    'id' => $group->user->id,
                    'name' => $group->user->name,
                    'header_img' => $group->user->header_img,
                ],

            ];
        }
        return [
            'id' => $this->resource->id,
            'img' => $this->resource->img,
            'name' => $this->resource->name,
            'brief' => $this->resource->detail->brief ?? '',
            'spu_id' => $this->resource->spu_id,
            'old_price' => $this->resource->old_price ?? 0,
            'sale_num' => $this->resource->sale_num ?? 0,
            'is_collection' => $this->is_collection,
            'stock' => $this->resource->stock ?? 0,
            'price' => $this->resource->price ?? "0",
            'is_pt' => $this->resource->is_pt ?? 0,
            'pt_num' => $this->resource->detail->pt_num ?? 0,
            'pt_price' => $this->resource->detail->pt_price,
            'is_xql' => $this->resource->is_xql ?? 0,
            'xql_price' => $this->resource->detail->xql_price,
            'is_sr' => $this->resource->is_sr ?? 0,
            'sr_price' => $this->resource->detail->sr_price,
            'imgs' => $this->resource->detail->imgs ?? [],
            'icons' => $this->resource->detail->icons ?? [],
            'parameter' => $this->resource->detail->parameter ?? [],
            'shop_price' => $this->resource->detail->shop_price,
            'shop_num' => $this->resource->detail->shop_num,
            'business_price' => $this->resource->detail->business_price,
            'business_num' => $this->resource->detail->business_num,
            'spec_name' => $this->resource->detail->spec_name??'',
            'type_name' => $this->resource->detail->type_name??'',
            'type_detail' => empty($this->resource->detail->type_detail) ? [] : explode(',',$this->resource->detail->type_detail),
            'tags' => empty($this->resource->detail->tags) ? [] : explode(',',$this->resource->detail->tags),
            'specs' => $specs,
            'groups' => $groups
        ];
    }
}
